package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.BaseMeterModelBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.dto.BaseMeterModelMap;
import com.koron.css2.serviceManage.mapper.BaseMeterModelMapper;
import com.koron.css2.serviceManage.queryBean.BaseMeterModelQueryBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * 水表型号查询
 * @author bin
 *
 */
public class WaterMeModelOpList implements ServerInterface{

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		try {
			BaseMeterModelQueryBean bean = JsonUtils.objectToPojo(req.getData(), BaseMeterModelQueryBean.class);
			BaseMeterModelMapper mapper = factory.getMapper(BaseMeterModelMapper.class);
			List<BaseMeterModelBean> list = mapper.selectMeterModelList(bean);
			List<BaseMeterModelMap> result = list.stream().parallel().map(t -> {

				BaseMeterModelMap baseMeterModelMap = new BaseMeterModelMap();
				baseMeterModelMap.setName(t.getModelName());
				baseMeterModelMap.setValue(t.getId());

				return baseMeterModelMap;
			}).collect(toList());
			info.setData(result);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
